package com.example.sys.module.service;

import com.example.core.init.SysInitService;
import com.example.core.mapping.Mapping;
import com.example.core.po.BaseAuthPo;
import com.example.core.service.BaseService;
import com.example.core.service.BaseServiceImpl;
import com.example.core.utils.ProxyClassUtil;
import com.example.core.utils.ReflectionKit;
import com.example.core.utils.StringUtils;
import com.example.sys.auth.constant.AuthConstant;
import com.example.sys.module.dao.SysModuleDao;
import com.example.sys.module.mapping.SysModuleMapping;
import com.example.sys.module.po.SysModule;
import com.example.sys.module.vo.SysModuleVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class SysModuleServiceImpl extends BaseServiceImpl<SysModuleDao, SysModule, SysModuleVo> implements SysModuleService, SysInitService {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public SysModule getModuleByService(String service) {
        return getBaseMapper().getModuleByService(service);
    }

    @Override
    public SysModule getModuleByDao(String dao) {
        return getBaseMapper().getModuleByDao(dao);
    }

    @Override
    public SysModule getModuleByPo(String po) {
        return getBaseMapper().getModuleByPo(po);
    }

    @Override
    public Class<? extends Mapping> getMappingClass() {
        return SysModuleMapping.class;
    }

    @Override
    public void init() throws Exception {
        Map<String, BaseService> baseServiceMap = getApplicationContext().getBeansOfType(BaseService.class);
        for (BaseService service : baseServiceMap.values()) {
            Class serviceClass = ProxyClassUtil.getRealClass(service);
            Class daoClass = ReflectionKit.getSuperClassGenricType(serviceClass, 0);
            Class poClass = ReflectionKit.getSuperClassGenricType(serviceClass, 1);
            Class voClass = ReflectionKit.getSuperClassGenricType(serviceClass, 2);
            String baseRole = StringUtils.camel2under(poClass.getSimpleName()).toUpperCase();
            SysModule sysModule = this.getModuleByService(serviceClass.getName());
            if (sysModule == null) {
                sysModule = new SysModule();
                sysModule.setService(serviceClass.getName());
                sysModule.setDao(daoClass.getName());
                sysModule.setPo(poClass.getName());
                sysModule.setVo(voClass.getName());
                sysModule.setBaseRole(baseRole);
                sysModule.setDefaultRole(baseRole + AuthConstant.DEFAULT_ROLE);
                sysModule.setAddRole(baseRole + AuthConstant.ADD_ROLE);
                sysModule.setDelRole(baseRole + AuthConstant.DELETE_ROLE);
                sysModule.setEditRole(baseRole + AuthConstant.EDIT_ROLE);
                sysModule.setViewRole(baseRole + AuthConstant.VIEW_ROLE);
                sysModule.setAuth(BaseAuthPo.class.isAssignableFrom(poClass));
                sysModule.recalculateFields();
                saveOrUpdate(sysModule);
            }
        }
    }

    @Override
    public void start() throws Exception {

    }

    @Override
    public void destroy() throws Exception {

    }
}
